package com.logic.landseaserver.ws;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.service.IAnswerService;
import com.logic.landseaserver.ws.dto.AnswerDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by Drake on 2017/4/19.
 */
@Path("/answer")
@Api(value="answer")
public class AnswerRest {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
	@Autowired
	private IAnswerService iAnswerServiceImpl;
	
    //填写问卷答案
	@POST
	@Path("/createSurveyAnswer")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a survey answer",position = 4)
	public Response createSurveyAnswer(@ApiParam(value = "param", required = true) AnswerDTO answerDTO) {
		CommonResultMap map = new CommonResultMap();
        try {
        	iAnswerServiceImpl.insertAnswer(answerDTO);
            map.setData(answerDTO);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("createSurveyAnswer error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createSurveyAnswer error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	//查询问卷答案
    @POST
    @Path("answers/{orderId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get answers by order id", position = 1)
    public Response retrieveAnswersByOrderCode(
            @ApiParam(value = "param", required = true) @PathParam("orderId") Integer orderId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(iAnswerServiceImpl.selectAnswersByOrderId(orderId));
        } catch (Exception e) {
            logger.error("retrieveAnswersByOrderCode error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    //编辑问卷答案
    @PUT
	@Path("/editAnswers")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Edit answers ")
    public Response updateOrder(@ApiParam(value = "param", required = true) AnswerDTO answerDTO) {
        CommonResultMap map = new CommonResultMap();
        try {
        	iAnswerServiceImpl.editAnswers(answerDTO);
            map.setResult(CommonResult.SUCCESS,"edit order");
        } catch (Exception e) {
            logger.error("updateOrder error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
